package com.xzm.project.ui.activity;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Space;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;


/**
 * author : xuzongmeng
 * date   : 2019/8/21
 * desc   : 动态线性布局
 */
public class DynamicLinearLayoutActivity extends AppCompatActivity {
    private LinearLayout rootView;
    private Button btnTest1;
    private Button btnTest2;
    private Button btnTest3;
    private Button btnTest4;
    private Button btnTest5;
    private Button btnTest6;

    public Context mContext;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext=this;
//        setContentView(R.layout.activity_dynamic_linearlayout);
        setContentView(getLayoutRootView());
        initView(savedInstanceState);
        initListener();
    }
    /**
     * 视图根容器  根容器必须设置 orientation
     */
    private View getLayoutRootView() {
        rootView = new LinearLayout(mContext);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        rootView.setOrientation(LinearLayout.VERTICAL);
        rootView.setLayoutParams(layoutParams);
        return rootView;
    }
    /**
     * 初始化Params
     */
    private LinearLayout.LayoutParams getLinearParams() {
        LinearLayout.LayoutParams params = null;
        if (params == null) {
            params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        }
        return params;
    }



    private int idrelativeLayout1 = 0x000111;
    private int iDbtnTest1 = 0x001;
    private int iDbtnTest2 = 0x002;
    private int iDbtnTest3 = 0x003;

    private int idrelativeLayout2 = 0x000222;
    private int iDbtnTest4 = 0x004;
    private int iDbtnTest5 = 0x005;
    private int iDbtnTest6 = 0x006;


    protected void initView(Bundle savedInstanceState) {
        //第一个子布局容器
        LinearLayout linearLayout1 = new LinearLayout(mContext);
        LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 500);
//        linearLayout1.setOrientation(LinearLayout.HORIZONTAL);
        linearLayout1.setBackgroundColor(Color.RED);
        linearLayout1.setId(idrelativeLayout1);
        linearLayout1.setLayoutParams(layoutParams1);


        btnTest1 = new Button(mContext);
        btnTest1.setId(iDbtnTest1);
        btnTest1.setText("按钮1");



        btnTest2 = new Button(mContext);
        btnTest2.setId(iDbtnTest2);
        btnTest2.setText("按钮2");
        /*可以嵌套这一层View*/
        LinearLayout nestLinearLayout = new LinearLayout(mContext);
        LinearLayout.LayoutParams nestLayoutParams1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);

        nestLayoutParams1.weight=1f;
//        nestLinearLayout.setWeightSum(1f);
        nestLinearLayout.setGravity(Gravity.CENTER);
        nestLinearLayout.setLayoutParams(nestLayoutParams1);
        nestLinearLayout.addView(btnTest2);


        btnTest3 = new Button(mContext);
        btnTest3.setId(iDbtnTest3);
        btnTest3.setText("按钮3");

        LinearLayout.LayoutParams LinearParams1 = getLinearParams();
        LinearLayout.LayoutParams LinearParams2 = getLinearParams();
        LinearLayout.LayoutParams LinearParams3 = getLinearParams();

        btnTest1.setLayoutParams(LinearParams1);
        btnTest2.setLayoutParams(LinearParams2);
        btnTest3.setLayoutParams(LinearParams3);


        linearLayout1.addView(btnTest1);
        linearLayout1.addView(nestLinearLayout);
        linearLayout1.addView(btnTest3);

        rootView.addView(linearLayout1);
        //第二个子布局容器
        LinearLayout linearLayout2 = new LinearLayout(mContext);
        LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        linearLayout2.setBackgroundColor(Color.GREEN);
        linearLayout2.setOrientation(LinearLayout.HORIZONTAL);
        linearLayout2.setId(idrelativeLayout2);
        linearLayout2.setLayoutParams(layoutParams2);




        btnTest4 = new Button(mContext);
        btnTest4.setId(iDbtnTest4);
        btnTest4.setText("按钮4");

        LinearLayout.LayoutParams nestLayoutParams2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        Space nestSpace1 = new Space(mContext);
        nestLayoutParams2.weight=1f;
        nestSpace1.setLayoutParams(nestLayoutParams2);

        btnTest5 = new Button(mContext);
        btnTest5.setId(iDbtnTest5);
        btnTest5.setText("按钮5");

        Space nestSpace2 = new Space(mContext);
        nestSpace2.setLayoutParams(nestLayoutParams2);


        btnTest6 = new Button(mContext);
        btnTest6.setId(iDbtnTest6);
        btnTest6.setText("按钮6");



        LinearLayout.LayoutParams layoutParams4 = getLinearParams();
        LinearLayout.LayoutParams layoutParams5 = getLinearParams();
        LinearLayout.LayoutParams layoutParams6 = getLinearParams();


        btnTest4.setLayoutParams(layoutParams4);
        btnTest5.setLayoutParams(layoutParams5);
        btnTest6.setLayoutParams(layoutParams6);

        linearLayout2.addView(btnTest4);
        linearLayout2.addView(nestSpace1);
        linearLayout2.addView(btnTest5);
        linearLayout2.addView(nestSpace2);
        linearLayout2.addView(btnTest6);

        rootView.addView(linearLayout2);

    }

    protected void initListener() {
        btnTest1.setOnClickListener(v -> Toast.makeText(mContext, "btnTest1", Toast.LENGTH_SHORT).show());
        btnTest2.setOnClickListener(v -> Toast.makeText(mContext, "btnTest2", Toast.LENGTH_SHORT).show());
        btnTest3.setOnClickListener(v -> Toast.makeText(mContext, "btnTest3", Toast.LENGTH_SHORT).show());
        btnTest4.setOnClickListener(v -> Toast.makeText(mContext, "btnTest4", Toast.LENGTH_SHORT).show());
        btnTest5.setOnClickListener(v -> Toast.makeText(mContext, "btnTest5", Toast.LENGTH_SHORT).show());
        btnTest6.setOnClickListener(v -> Toast.makeText(mContext, "btnTest6", Toast.LENGTH_SHORT).show());
    }
}
